package LeetCode.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/15
 * @time: 10:16
 */
public class Test202快乐数 {
    public static void main(String[] args) {
        System.out.println(isHappy(18));
    }

    public static boolean isHappy(int n) {
         if (n <= 0) {
             return false;
         }
        Set<Integer> set = new HashSet<>();
         while (n != 1) {
             set.add(n);
             n = getBow(n);
             if (set.contains(n)) {
                 return false;
             }
         }
         return true;
    }

    public static int getBow(int n) {
        int sum = 0;
        while (n != 0) {
            sum += Math.pow( n % 10, 2);
            n = n / 10;
        }
        return sum;
    }

}
